package com.jdym.controller;

import com.jdym.R;
import com.jdym.entity.Uploadfile;
import com.jdym.service.UploadfileService;
import com.jdym.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 上传资料 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-09-20
 */
@RestController
@RequestMapping("/uploadfile")
public class UploadfileController {
    @Autowired
    UploadfileService uploadfileService;
        @RequestMapping("/image")
        @ResponseBody
        public R image(@RequestParam("upfile") MultipartFile file){
            //调用工具类完成文件上传
            System.out.println("file:"+file);
            String imagePath = UploadUtils.upload(file);
            System.out.println(imagePath);

            if(imagePath!=null){
               return R.ok().data("img",imagePath);
            }else {
                return R.ok().data("img","上传失败");
            }

        }

        @RequestMapping("/saveFiles")
          public R saveFiles(String pawn,String img0,String img1,String img2,String img3,String img4,String img5,int loanId){
            Uploadfile uploadfile=new Uploadfile();
            uploadfile.setPawn(pawn);
            if(img0!=null){
                uploadfile.setUpfileUrl(img0);
            }
            if(img1!=null)
            {
                uploadfile.setIdentity1(img1);
            }
            if(img2!=null)
            {
                uploadfile.setIdentity2(img2);
            }
            if(img3!=null)
            {
                uploadfile.setCertificate(img3);
            }
            if(img4!=null)
            {
                uploadfile.setCurrentAccount(img4);
            }
            if(img5!=null)
            {
                uploadfile.setCreditReport(img5);
            }
            uploadfile.setLoanid(loanId);
            uploadfileService.getBaseMapper().insert(uploadfile);
            return R.ok().data("msg","上传成功");
        }


}

